package com.rjt.common.util;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
/**  
* @comment
* @author renjiangtao 
* @date 2018年8月12日  
* @version 1.0  
*/
public class HttpClientUtil {
	public static String doPost(String url,String json,String ck){
		   //开启代理，fiddler
	      /*  HttpHost proxy = new HttpHost("127.0.0.1",8888);
	        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();*/
	        HttpPost post = new HttpPost(url);
	        CloseableHttpClient httpclient = HttpClientBuilder.create()
	        		/*.setDefaultRequestConfig(requestConfig)*/
	        		.build();
	        String result = null;
	        try {
	            StringEntity s = new StringEntity(json.toString());
	            s.setContentEncoding("UTF-8");
	            s.setContentType("application/json");//发送json数据需要设置contentType
	            post.setHeader("cookie", ck);
	            post.addHeader("User-Agent", "test");
	            post.setEntity(s);
	            HttpResponse res = httpclient.execute(post);
	             result = EntityUtils.toString(res.getEntity());// 返回json格式：
	           
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	        return result;
	    }

}
